#!/bin/bash

set -e

if [ "$1" = configure ]; then

	INITSYS=$(ls -al /proc/1/exe | awk -F' ' '{print $NF}' | awk -F'/' '{print $NF}')

	if [ "systemd" == "$INITSYS" ]; then
		if [ -e /etc/systemd/system/hoptodesk.service ]; then
			rm /etc/systemd/system/hoptodesk.service
		fi
		version=$(python3 -V 2>&1 | grep -Po '(?<=Python )(.+)')
		parsedVersion=$(echo "${version//./}")
		cp /usr/share/hoptodesk/files/systemd/hoptodesk.service /etc/systemd/system/hoptodesk.service
		systemctl daemon-reload
		systemctl enable hoptodesk
		systemctl start hoptodesk
	elif [ "init" == "$INITSYS" ]; then
		insserv -dv hoptodesk
		service hoptodesk start
    fi
fi